﻿@using System.Web.Mvc.Html;
@using System.Web.WebPages.Html;
@using System.Web.Mvc;
@using DddCms.Models.UIComponent.Form;
@using DddCms.Presentation.UIComponent.Form;
@using DddCms.Presentation.Areas.DddCms.Models.UIComponent.Tooltip;
@using DddCms.Models.UIComponent.Window;
@*
    Permet la génération de formulaire    
*@
@helper Form( Func<FormViewModelBuilder,FormViewModelBuilder> funcBuilder)
{
    
    FormViewModel viewmodel = funcBuilder(new FormViewModelBuilder()).Build();
    @WindowHelpers.WindowFunc(w => w
        .Width(viewmodel.Width)
        .Position(viewmodel.Position)
        .Content(
    @<text>       
    <center><h2>@viewmodel.Title</h2></center>
    @{
        string height = "auto";
        }
    @if (viewmodel.Height != 0)
    {
         height = string.Format("{0}px", viewmodel.Height);
    }
    <div class="form"  style="height:@height;">
        @FormRow(viewmodel)  
        @if (viewmodel.Error != null)
        {
              @WindowHelpers.WindowFunc(wr => wr
                      .Style("error-message")
                      .Width("100%")
                         .Position(WindowPositionViewModel.FullSize)
                    .Content(
                @viewmodel.Error
            ))
        }
        <table class="form-table-field" cellpadding="0" cellspacing="0">
             <tr class="form-row" style="height:10px;">
                <td class="form-field"   colspan="@viewmodel.ColSpan(1)">
                </td>
            </tr>
            <tr class="form-row">
                <td class="form-field" colspan="@viewmodel.ColSpan(1)">
                    <input class="submit-form" type="submit" value="Valider" style="margin-top:10px;margin-bottom:15px;margin-right: 35px;float:right;" />
                </td>
            </tr>
        </table>
    </div>
    </text>
    ))
}

@helper FormRenderField(FormFieldViewModel fieldView, WindowPositionViewModel position)
    {
    <table  style="table-layout:fixed; position: relative;" width="100%" height="100%" border="1" cellpadding="0" cellspacing="0">
        <tr class="form-tr-field">
            @switch (fieldView.FormField)
            {
                case FormField.Text:
                    <td class="form-field-title">
                        @MvcHtmlString.Create(fieldView.Title) :
                    </td>
                    <td class="form-field-value">
                        
                        @WindowHelpers.WindowFunc(w => w
                            .Style("form-message")
                            .Position(WindowPositionViewModel.FullSize)
                            .Width("100%")
                            .Content(
                                @<text>
                                    <table style="overflow:visible;width:100%;">
                                        <tr style="overflow:visible;">
                                            <td style="overflow:visible;width:100%;">
                                                @MvcHtmlString.Create(fieldView.EditItem.Value.ToString())
                                            </td>
                                            <td class="field-validation-error-td" style=" vertical-align:middle;">
                                                @TooltipHelpers.Tooltip(t => t
                                                    .Message("coucou")
                                                    .WindowStyle("error-message")
                                                    .Content(fieldView.EditItem.Error)
                                                    .Position(TooltipPosition.BottomRight))
                                            </td>
                                        </tr>
                                    </table>
                                </text>))
                        
                    </td>
                   break;
                case FormField.Checkbox:
                    <td class="form-field-title">
                        
                    </td>
                    <td class="form-field-value">
                        <div class="form-field-checkbox">
                            @WindowHelpers.WindowFunc(w => w
                                .Style("form-message")
                                .Position(position)
                                .Content(@<text>@fieldView.EditItem.Value @fieldView.Title</text>))
                        </div>
                    </td>
        
                       break;

            }
        </tr>
    </table>
}
@*
    Les lignes de champs son regroupé en "form-table-row"  
*@
@helper FormRow(FormViewModel viewmodel)
    {
        foreach (object itemFormRow in viewmodel)
        {
            if (itemFormRow is FormRowViewModel)
            {
                FormRowViewModel model = itemFormRow as FormRowViewModel;
                if (model.IsTheNewOfTableField)
                {
                @:<table class="form-table-field" width="100%" cellpadding="0" cellspacing="0">
            }
            <tr class="form-row">
                @foreach (FormFieldViewModel field in model.Fields)
                {
                    <td class="form-field" colspan="@viewmodel.ColSpan(model.Fields.Count)">
                        @FormRenderField(field, viewmodel.Position)
                    </td>
                }
            </tr> 
                if (model.IsTheLastTableField)
                {
                @:</table>
            }
            }
            else if (itemFormRow is FormMessageViewModel)
            {
                FormMessageViewModel model = itemFormRow as FormMessageViewModel;
                if (model.IsAfterARowField)
                {
                @:</table>
            }
            <div class="form-info-text" >
                <div class="form-info-text-content" >
                    @WindowHelpers.WindowFunc(w => w
                    .Style("form-message")
                    .Position(WindowPositionViewModel.FullSize)
                    .Width("100%")
                    .Content(@<text><div>@MvcHtmlString.Create(model.Message)</div></text>))
                </div>
            </div>
            
            } 
      @*  if (viewmodel.Rows.ContainsKey(i))
        {
            FormRowViewModel model = viewmodel.Rows[i];
            int colspan = viewmodel.Rows.Max(f => f.Value.Fields.Count) - model.Fields.Count + 1;

            if (viewmodel.PreIsFieldRow())
            {
                <text><table style="table-layout:fixed;" width="@viewmodel.Width" height="100%" border="1" cellpadding="0" cellspacing="0"></text>
                currentTab = true;
            }
            <tr class="form-row">
                @foreach (FormFieldViewModel item in model.Fields)
                {
                    <td class="form-field" colspan="@colspan">
                        @FormRenderField(item)
                    </td>
                }
            </tr> 
        }
        else
        {
            string model = viewmodel.Messages[i];

            if (currentTab)
            {
                @:</table>
                currentTab = false;
            }
            <div class="form-info-text" style="">
                @WindowHelpers.WindowFunc(w => w
                .Style("form-message")
                .Content(@<div style="margin: 6px;">@model</div>))
                   
            </div>
            


    }
    if (currentTab)
    {
        @:</table>
        currentTab = false;
    }       *@ }
   
}

@helper FormRowRender(FormRowViewModel visitor)
    {
    
}